﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.ViewModel.Day
{
    public class ProfitAndLossDistribution
    {
        /// <summary>
        /// 左侧菜单显示信息
        /// </summary>
        public LeftMenuInfos LeftMenuInfos { get; set; }

        /// <summary>
        /// 查询的交易员
        /// </summary>
        public Models.Sys.Operator Operator { get; set; }
        /// <summary>
        /// 查询日期
        /// </summary>
        public DateTime Pdate { get; set; }
        /// <summary>
        /// 查询日期
        /// </summary>
        public string HPdate { get; set; }
        /// <summary>
        /// 月累计
        /// </summary>
        public Models.View.OperatorMonthSum OperatorMonthSum { get; set; }
        /// <summary>
        /// 当日交易信息
        /// </summary>
        public Models.View.OperatorDayChange OperatorDayChange { get; set; }
        /// <summary>
        /// 分时情况 盈利
        /// </summary>
        public Models.View.OperatorDayTradeInfo WinTimePie { get; set; }
        /// <summary>
        /// 分时情况 盈利 数据
        /// </summary>
        public Models.View.OperatorDayTradeInfoData WinSumData { get; set; }
        /// <summary>
        /// 分时情况 亏损
        /// </summary>
        public Models.View.OperatorDayTradeInfo LoseTimePie { get; set; }
        /// <summary>
        /// 分时情况 亏损 数据
        /// </summary>
        public Models.View.OperatorDayTradeInfoData LostSumData { get; set; }

        /// <summary>
        /// 品种交易情况 盈利
        /// </summary>
        public List<Models.View.OperatorDayProductInfo> WinProductInfos { get; set; }
        /// <summary>
        /// 品种交易情况 盈利 数据
        /// </summary>
        public Models.View.OperatorDayTradeInfoData WinWinSumData { get; set; }
        /// <summary>
        /// 品种交易情况 亏损
        /// </summary>
        public List<Models.View.OperatorDayProductInfo> LostWinInfos { get; set; }
        /// <summary>
        /// 品种交易情况 亏损 数据
        /// </summary>
        public Models.View.OperatorDayTradeInfoData LostWinSumData { get; set; }
        /// <summary>
        /// 多空方向 盈利
        /// </summary>
        public Models.View.OperatorDayDirectionInfo WinDirection { get; set; }
        /// <summary>
        /// 多空方向 盈利 数据
        /// </summary>
        public Models.View.OperatorDayTradeInfoData WinDirectionSumData { get; set; }
        /// <summary>
        /// 多空方向 亏损
        /// </summary>
        public Models.View.OperatorDayDirectionInfo LostDirection { get; set; }
        /// <summary>
        /// 多空方向 亏损 数据
        /// </summary>
        public Models.View.OperatorDayTradeInfoData LostDirectionSumData { get; set; }
        /// <summary>
        /// 按收益情况 盈利
        /// </summary>
        public Models.View.OperatorDayWinNum WinNum { get; set; }
        /// <summary>
        /// 按收益情况 盈利 数据
        /// </summary>
        public Models.View.OperatorDayTradeInfoData WinNumSumData { get; set; }
        /// <summary>
        /// 按收益情况 亏损
        /// </summary>
        public Models.View.OperatorDayWinNum LostNum { get; set; }
        /// <summary>
        /// 按收益情况 亏损 数据
        /// </summary>
        public Models.View.OperatorDayTradeInfoData LostNumSumData { get; set; }

        public string JS { get; set; }

        #region 定义一些使用到的接口
        private Interface.Sys.IOperator IOperator = null;
        private Interface.View.IOperatorMonthSum IOperatorMonthSum = null;
        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.View.IOperatorDayChange IOperatorDayChange = null;
        private Interface.View.IOperatorDayTradeInfo IOperatorDayTradeInfo = null;
        private Interface.View.IOperatorDayProductInfo IOperatorDayProductInfo = null;
        private Interface.View.IOperatorDayDirectionInfo IOperatorDayDirectionInfo = null;
        private Interface.View.IOperatorDayWinNum IOperatorDayWinNum = null;
        #endregion


        public ProfitAndLossDistribution(int uid, string queryDate, DB.DBManager db)
        {
            InitInterface(db);
            this.Operator = IOperator.GetModel(uid);

            string _year = queryDate.ToString().Substring(0, 4);
            string _month = queryDate.ToString().Substring(4, 2);
            string _day = queryDate.ToString().Substring(6, 2);
            string _pdate = _year + "-" + _month + "-" + _day;
            this.Pdate = Convert.ToDateTime(_pdate);
            this.HPdate = _pdate;

            Models.Sys.TradeHistory th = ITradeHistory.GetSettlementModel(queryDate);

            this.OperatorMonthSum = IOperatorMonthSum.GetOperatorMonthSum(this.Operator.Id, th.MPdate, th.YPdate);

            this.OperatorDayChange = IOperatorDayChange.GetOperatorDayChange(this.Operator.Id, th.Id);

            #region 分时情况 盈利
            this.WinTimePie = IOperatorDayTradeInfo.GetOperatorDayTradeInfoInWinOrLose(th.Id, this.Operator.Id, "sumwin", true);
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.BenefitTimePie(this.WinTimePie, "showflash2", "盈利示意图", "BenefitTimePie2");//ok
            this.WinSumData.BillCount = this.WinTimePie.Datas.Sum(a => a.BillCount);
            this.WinSumData.SumValue = this.WinTimePie.Datas.Sum(a => a.SumValue);
            #endregion

            #region 分时情况 亏损
            this.LoseTimePie = IOperatorDayTradeInfo.GetOperatorDayTradeInfoInWinOrLose(th.Id, this.Operator.Id, "sumlost", false);
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.BenefitTimePie(this.LoseTimePie, "showflash", "亏损示意图", "BenefitTimePie");//ok
            this.LostSumData.BillCount = this.LoseTimePie.Datas.Sum(a => a.BillCount);
            this.LostSumData.SumValue = this.LoseTimePie.Datas.Sum(a => a.SumValue);
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.WinAndLose(this.OperatorDayChange);//ok
            #endregion

            #region 品种交易情况 盈利
            var infos = IOperatorDayProductInfo.GetOperatorDayProductInfo(th.Id, this.Operator.Id, true);
            List<Models.View.OperatorDayProductInfo> _infos = new List<Models.View.OperatorDayProductInfo>();
            foreach (Models.View.OperatorDayProductInfo tempinfo in infos)
            {
                var query = _infos.Where(a => a.ProductId.Equals(tempinfo.ProductId));
                if (query.Count() > 0)
                {
                    query.First().AvgBillProfit += tempinfo.AvgBillProfit;
                    query.First().BillCount += tempinfo.BillCount;
                    query.First().DayCount += tempinfo.DayCount;
                    query.First().Free += tempinfo.Free;
                    query.First().Percent += tempinfo.Percent;
                    query.First().Profit += tempinfo.Profit;
                }
                else
                {
                    _infos.Add(tempinfo);
                }
            }
            this.WinProductInfos = _infos;
            this.WinWinSumData.BillCount = _infos.Sum(a => a.BillCount);
            this.WinWinSumData.SumValue = _infos.Sum(a => a.Profit);
            #endregion

            #region 品种交易情况 亏损
            var infos2 = IOperatorDayProductInfo.GetOperatorDayProductInfo(th.Id, this.Operator.Id, false);
            List<Models.View.OperatorDayProductInfo> _infos2 = new List<Models.View.OperatorDayProductInfo>();
            foreach (Models.View.OperatorDayProductInfo tempinfo in infos2)
            {
                var query = _infos2.Where(a => a.ProductId.Equals(tempinfo.ProductId));
                if (query.Count() > 0)
                {
                    query.First().AvgBillProfit += tempinfo.AvgBillProfit;
                    query.First().BillCount += tempinfo.BillCount;
                    query.First().DayCount += tempinfo.DayCount;
                    query.First().Free += tempinfo.Free;
                    query.First().Profit += tempinfo.Profit;
                    query.First().Percent += tempinfo.Percent;
                }
                else
                {
                    _infos2.Add(tempinfo);
                }
            }
            this.LostWinInfos = _infos2;
            this.LostWinSumData.BillCount = _infos2.Sum(a => a.BillCount);
            this.LostWinSumData.SumValue = _infos2.Sum(a => a.Profit);
            #endregion

            #region 多空方向 盈利
            this.WinDirection = IOperatorDayDirectionInfo.GetOperatorDayDirectionInfo(th.Id, this.Operator.Id, true);
            this.WinDirectionSumData.BillCount = this.WinDirection.Datas.Sum(a => a.BillCount);
            this.WinDirectionSumData.SumValue = this.WinDirection.Datas.Sum(a => a.SumValue);
            #endregion

            #region 多空方向 亏损
            this.LostDirection = IOperatorDayDirectionInfo.GetOperatorDayDirectionInfo(th.Id, this.Operator.Id, false);
            this.LostDirectionSumData.BillCount = this.LostDirection.Datas.Sum(a => a.BillCount);
            this.LostDirectionSumData.SumValue = this.LostDirection.Datas.Sum(a => a.SumValue);
            #endregion

            #region 按收益情况 盈利
            this.WinNum = IOperatorDayWinNum.GetOperatorDayWinNum(th.Id, this.Operator.Id, true);
            this.WinNumSumData.BillCount = this.WinNum.Datas.Sum(a => a.BillCount);
            this.WinNumSumData.SumValue = this.WinNum.Datas.Sum(a => a.SumValue);
            #endregion

            #region 按收益情况 亏损
            this.LostNum = IOperatorDayWinNum.GetOperatorDayWinNum(th.Id, this.Operator.Id, false);
            this.LostNumSumData.BillCount = this.LostNum.Datas.Sum(a => a.BillCount);
            this.LostNumSumData.SumValue = this.LostNum.Datas.Sum(a => a.SumValue);
            #endregion

            DisposeInterface();
        }




        private void InitInterface(DB.DBManager db)
        {
            #region 初始化接口
            IOperator = new Service.Sys.ServiceOperator(db);
            IOperatorMonthSum = new Service.View.ServiceOperatorMonthSum(db);
            ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IOperatorDayChange = new Service.View.ServiceOperatorDayChange(db);
            IOperatorDayTradeInfo = new Service.View.ServiceOperatorDayTradeInfo(db);
            IOperatorDayProductInfo = new Service.View.ServiceOperatorDayProductInfo(db);
            IOperatorDayDirectionInfo = new Service.View.ServiceOperatorDayDirectionInfo(db);
            IOperatorDayWinNum = new Service.View.ServiceOperatorDayWinNum(db);
            #endregion
        }
        /// <summary>
        /// 释放接口
        /// </summary>
        private void DisposeInterface()
        {
            #region 释放接口
            IOperator = null;
            IOperatorMonthSum = null;
            ITradeHistory = null;
            IOperatorDayChange = null;
            IOperatorDayTradeInfo = null;
            IOperatorDayProductInfo = null;
            IOperatorDayDirectionInfo = null;
            IOperatorDayWinNum = null;
            #endregion
        }
    }
}
